<?php
/**
 * Created by JetBrains PhpStorm.
 * User: CAIXUDONG
 * Date: 11-8-23
 * Time: 下午3:13
 */

class FTP
{
    public $conn = ''; //R ftp资源;

//R 1:成功;2:无法连接ftp;3:用户错误;
    public function __construct($ftp_config)
    {
        if ($this->conn = ftp_connect($ftp_config['ip'], 21)) {
            if (ftp_login($this->conn, $ftp_config['username'], $ftp_config['password'])) {
                if (!empty($ftp_config['path'])) {
                    @ftp_chdir($this->conn, $ftp_config['path']);
                }
                ftp_pasv($this->conn, true); //R 启用被动模式;
            } else {
                $error = 'FTP用户名密码错误';
                $data = array(
                    "statusCode"=>"520",
                    "message"=>$error,
                );
                echo Response::JSON($data);
                exit;
            }
        } else {
            $error = '无法连接FTP：'.$ftp_config['ip'];
            $data = array(
                "statusCode"=>"510",
                "message"=>$error,
            );
            echo Response::JSON($data);
            exit;
        }
    }

//R 切换目录;
    public function cd($dir)
    {
        if (@ftp_chdir($this->conn, $dir)) {
            return true;
        } else {
            return false;
        }
    }

//R 返回当前路劲;
    public function pwd()
    {
        return @ftp_pwd($this->conn);
    }

//R 创建目录
	public function mkdir($dir)
	{
        $this->cd('/');
        $dir_arr = explode('/', trim($dir,'/'));
        foreach ($dir_arr as $v)
        {
            if ($this->conn){
                @ftp_mkdir($this->conn, $v);
                $this->cd($v);
            }
        }
	}

//R 删除目录
    public function rmdir($directory)
    {
        return @ftp_rmdir($this->conn, $directory);
    }

//R 上传文件;
    public function put($localFile, $remoteFile = '')
    {
        if ($remoteFile == '') {
            $remoteFile = basename($localFile);
        }
        $res = ftp_nb_put($this->conn, $remoteFile, $localFile, FTP_BINARY);
        while ($res == FTP_MOREDATA) {
            $res = ftp_nb_continue($this->conn);
        }
        if ($res == FTP_FINISHED) {
            return true;
        } elseif ($res == FTP_FAILED) {
            return false;
        }
    }

//R 下载文件;
    public function get($remoteFile, $localFile = '')
    {
        if ($localFile == '') {
            $localFile = basename($remoteFile);
        }
        if (ftp_get($this->conn, $localFile, $remoteFile, FTP_BINARY)) {
            $flag = true;
        } else {
            $flag = false;
        }
        return $flag;
    }

//R 文件大小;
    public function size($file)
    {
        return ftp_size($this->conn, $file);
    }

//R 文件是否存在;
    public function isFile($file)
    {
        if ($this->size($file) >= 0) {
            return true;
        } else {
            return false;
        }
    }

//R 文件时间
    public function fileTime($file)
    {
        return ftp_mdtm($this->conn, $file);
    }

//R 删除文件;
    public function unlink($file)
    {
        return ftp_delete($this->conn, $file);
    }

    public function nlist($dir = '/')
    {
        return ftp_nlist($this->conn, $dir);
    }

//R 关闭连接;
    public function close()
    {
        return ftp_close($this->conn);
    }
}